//*********基础功能重新封装**************//
function apiUrlGet( url , data , func){
    var time = getTimestamp();
    api.ajax({
        url: url,
        method: 'GET',
        returnAll:true,
        headers: {
            "token":createToken(time)
            ,"nowtime": time
            ,"Cookie": cookie()
            ,"sessId": getSessionId()
            ,"access-token": getAccessToken()
            ,"X-AUTH-TOKEN": getAuthToken()
        },
        dataType:"json",
        data:  {values:data}
    }, function(ret, err) {
        var data ,code ;
        // logJson(err,url + " err_:")
        // logJson(ret,url + " ret_:")
        if (!isTrue(err)) {
            data = ret.body;
            code = ret.statusCode
        }else {
            // log(err.msg,url + " Error_msg:");
            data = err.body;
            code = err.statusCode
        }

        if (isTrue(ret)) {
            var cookie = getCookie(ret.headers)
            if(typeof cookie == "string"){setLocData("cookie",cookie);}
            getAccessTokenFromHeader(ret.headers)
            apiDealWithResult(code,data,func)
        }
    });
}


function apiGet( url , data , func){
    var time = getTimestamp();
    api.ajax({
        url: baseApi + url,
        method: 'GET',
        returnAll:true,
        headers: {
            "token":createToken(time)
            ,"nowtime": time
            ,"Cookie": cookie()
            ,"sessId": getSessionId()
            ,"access-token": getAccessToken()
            ,"X-AUTH-TOKEN": getAuthToken()
        },
        dataType:"json",
        data:  {values:data}
    }, function(ret, err) {
        var data ,code ;
        // log(err.body,url + " err_:")
        // logJson(ret,url + " ret_:")
        if (!isTrue(err)) {
            data = ret.body;
            code = ret.statusCode
        }else {
            // log(err.msg,url + " Error_msg:");
            data = err.body;
            code = err.statusCode
        }
        logJson(ret)
        if (isTrue(ret)) {

            var cookie = getCookie(ret.headers);
            if(typeof cookie == "string"){setLocData("cookie",cookie);}
            getAccessTokenFromHeader(ret.headers)
            apiDealWithResult(code,data,func)
        }
    });
}

function apiPost( url , data ,func){
    var time = getTimestamp();
    var method = "POST"
    if (isTrue(data.id)) {
        method = "PATCH"
        url += "/" + data.id
    }
    api.ajax({
        url: baseApi + url,
        method: method,
        returnAll:true,
        headers: {
            "token":createToken(time)
            ,"nowtime": time
            ,"Cookie": cookie()
            ,"sessId": getSessionId()
            ,"access-token": getAccessToken()
            ,"X-AUTH-TOKEN": getAuthToken()
        },
        dataType:"json",
        data:  {values:data}
    }, function(ret, err) {
        var data ,code;
        // logJson(err,url + " err_:")
        // logJson(ret,url + " ret_:")
        if (!isTrue(err)) {
            data = ret.body;
            code = ret.statusCode;
        }else {
            // log(err.msg,url + " Error_msg:");
            data = err.body;
            code = err.statusCode;
        }

        if (isTrue(ret)) {

            var cookie = getCookie(ret.headers)
            if(typeof cookie == "string"){setLocData("cookie",cookie);}
            getAccessTokenFromHeader(ret.headers)
            apiDealWithResult(code,data,func)
        }
    });
}

function apiDealWithResult(code,res,callback) {
    if (res === undefined) {
        showToast("网络请求异常");
        return false;
    }
    if(code >= 200 && code <300){
        if(code == 203){
            //未登录
            showToast(res.message);
            setTimeout(function () {
                goto("login");
            },1000)
            return  false;
        }
        //正常数据
        callback(res,code)
    }
    if(code == 301){
        //未登录
        showToast(res.message);
    }
    if(code == 404){
        //内容未找到
        showToast(res.message);
        goto("ERROR",{})
    }
    if(code == 400){
        //系统级错误
        showToast(res.message);
    }
    if(code == 500){
        //系统级错误
        showToast(res.message);
    }
    if(code == 400){
        //系统级错误
        showToast(res.message);
    }
}

function createToken(time) {
    return MD5(time+VI)
}
function createSessionId() {
    var time = getTimestamp();
    var sessId = MD5(time + VI + time)
    setLocData('sessid',sessId)
    return sessId;
}
function getSessionId() {
    var sessId = getLocData("sessid")
    if (!isTrue(sessId)) {
        sessId = createSessionId();
    }
    return sessId;
}

function getAccessToken() {
    var accessToken = getLocData("access_token")
    if (!isTrue(accessToken)) {
        accessToken = "app_" + createSessionId();
    }
    return accessToken;
}
function getAuthToken() {
    var accessToken = getLocData("auth_token")
    // if (!isTrue(accessToken)) {
    //     accessToken = "app_" + createSessionId();
    // }
    return accessToken;
}

function getAccessTokenFromHeader(headers){
    for (var i in headers) {
        if (i == "access-token") {
            if (isTrue(headers[i])) {
                setLocData("access_token",headers[i]);
            }
            return headers[i];
        }
    }
    var token = getSessionId();
    setLocData("access_token",token)
    return token;
}


function apiLoad(url , data , func){
    showLoading()
    apiGet(url , data ,function (res) {
        hideLoading();
        func(res)
    })
}
function apiLog(data){
    api.ajax({
        url: loggerUrl ,
        method: 'POST',
        returnAll:true,
        headers: {
            "token":api.deviceId
            ,"Cookie": cookie()
        },
        dataType:"json",
        data:  {values:{content:data}}
    }, function(ret, err) {
    });
}

function apiUpload( url , path ,func){
    log("进入apiGet,正在获取数据")
    var files = {Filedata:path}
    api.ajax({
        url: baseApi + url,
        method: 'POST',
        returnAll:true,
        headers: {
            "token":api.deviceId
            ,"Cookie": cookie()
        },
        dataType:"json",
        data:  {files:files}
    }, function(ret, err) {
        logHtml(ret)
        // var cookie = getCookie(ret.headers)
        // if(typeof cookie == "string"){setLocData("cookie",cookie);}
        // getAccessTokenFromHeader(ret.headers)
        // apiDealWithResult(ret.body,func)

        var data ,code;
        // logJson(err,url + " err_:")
        // logJson(ret,url + " ret_:")
        if (!isTrue(err)) {
            data = ret.body;
            code = ret.statusCode;
        }else {
            // log(err.msg,url + " Error_msg:");
            data = err.body;
            code = err.statusCode;
        }

        if (isTrue(ret)) {

            var cookie = getCookie(ret.headers)
            if(typeof cookie == "string"){setLocData("cookie",cookie);}
            getAccessTokenFromHeader(ret.headers)
            apiDealWithResult(code,data,func)
        }


    });
}

function getCookie(headers){
    var cookies = [];
    for (var i in headers) {
        if (i == "Set-Cookie") {
            var arr = headers[i].split(";")
            cookies = arr[0];
        }
    }
    return cookies;
}

function cookie(){return getLocData("cookie")}

